*Figures in Manuscript
cd "/Users/chengzhengquan/Desktop/globalization"
use wvs_data.dta, clear

*************************************************************
**Figure 1
*************************************************************
************************
*1A
hist prosocial, xtitle("Pooled Distribution of Prosociality") discrete ///
     percent fcolor(gs8) lcolor(gs3) ///
	 xlabel(1(1)6) ///
	 title("A") ///
	 scheme(s1mono) name(a, replace) 
graph save a, replace	

bys year country_id : egen prosocial_m = mean(prosocial)
bys year country_id : egen prosocial_sd = sd(prosocial)
bys year country_id : gen N = _N
gen prosocial_s = prosocial_sd / sqrt(N)
duplicates drop year country_id, force

cap drop tariff_w
merge m:1 countrycode year using tariff.dta, keepusing(tariff_w countryname) nogenerate
drop if tariff_w > 100
drop if countryname == ""

keep countrycode countryname prosocial_m prosocial_s tariff_w
drop if prosocial_m == . 

************************
*1C
drop if countryname == "Qatar"
gen z1 = prosocial_m + prosocial_s
gen z2 = prosocial_m - prosocial_s

sort prosocial_m
gen country = _n
labmask country, value(countryname)

twoway(rcap z1 z2 country, blwidth(*1)) /// 
	  (scatter prosocial_m country, msymbol(circle) mcolor(black) msize(*0.2)), ///
	  yline(3(0.5)5.5, lp(dot) lc(black*0.2)) ///
	  xline(1(1)55, lp(dot) lc(black*0.2)) ///
	  ylabel(3(0.5)5.5, labsize(*0.8)) ///
	  xlabel(1(1)55, valuelabel labsize(*0.5) angle(45)) legend(off) ///
	  title("C", position(l2)) xtitle("") ///
	  ytitle("Mean +/- s.e.") ///
	  scheme(s1mono) name(c, replace) 
graph save c, replace	

************************
*1B
format prosocial_m tariff_w %9.1f
rename countrycode scode

merge 1:1 scode using trans.dta,nogenerate
drop if scode == ""

merge 1:1 id using usdb.dta,nogenerate
drop if id == .

spmap prosocial_m using uscoord, id(id) fcolor(Heat) ///
			  ndfcolor(white) legstyle(1) /// 
			  legend(ring(1) position(3) size(*1) symxsize(*1.3) symysize(*1.3))  /// 
			  clmethod(eqint) clnumber(15) ///
			  title("B")  /// 
			  subtitle("World Distribution of Average Prosociality" " ", ring(1) position(6)) ///  
			  polyfirst name(b, replace)		
graph save b, replace	

*combine figures	 
gr combine a.gph b.gph, r(1) c(2) scheme(s1mono) saving(ab, replace)		
gr combine ab.gph c.gph, r(2) c(1) scheme(s1mono) saving(figure1, replace)

*************************************************************
**Figure 2
*************************************************************
use wvs_data.dta, clear

bys year country_id : egen prosocial_m = mean(prosocial)
duplicates drop year country_id, force

cap drop tariff_w tariff_s
merge m:1 countrycode year using tariff.dta, keepusing(tariff_w) nogenerate
drop if tariff_w > 100

keep countrycode prosocial_m tariff_w
drop if prosocial_m == . 

************************
*1A
format prosocial_m tariff_w %9.1f
rename countrycode scode

merge 1:1 scode using trans,nogenerate
drop if scode == ""

merge 1:1 id using usdb,nogenerate
drop if id == .

spmap tariff_w using uscoord, id(id) fcolor(Heat) ///
			  ndfcolor(white) legstyle(1) /// 
			  legend(ring(1) position(3) size(*1) symxsize(*1.3) symysize(*1.3))  /// 
			  clmethod(eqint) clnumber(15) ///
			  title("A")  /// 
			  subtitle("World Distribution of Weighted Tariff Rate (%)" " ", ring(1) position(6)) ///  
			  polyfirst name(a, replace)
graph save a, replace	

************************
*2B
format prosocial_m %9.1f
format tariff_w %9.0f
twoway (scatter prosocial_m tariff_w, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci prosocial_m tariff_w, level(95) lcolor(gray)), ///
	   legend(off) title("B") ///
	   xlabel(0(5)15) ///
	   ylabel(3(1)5.5) ///
	   ytitle("Average Level of Prosociality") ///
	   xtitle("Weighted Tariff Rate (%)") ///
	   scheme(s1color) name(b, replace) 
graph save b, replace

************************
*2C
use wvs_data.dta, clear
global control "female age i.age_g i.education i.marital i.child i.employment i.social_class i.income i.year"

estimates clear
local categories "prosocial"
foreach y of varlist `categories'{
	qui reg `y' lntw, cluster(countrycode)
	qui estimates store `y'1

	qui reg `y' lntw $control, cluster(countrycode)
	qui estimates store `y'2

	qui reg `y' lntw $control gdp, cluster(countrycode)
	qui estimates store `y'3

}

coefplot (prosocial1, label("No Control Var.")) ///
		 (prosocial2, label("Control Individual Var.")) ///
		 (prosocial3, label("Control All Var.")) ///
		 , keep(lntw) vertical ///
		 title("C") ///
		 ytitle("Effect Size") ///
		 ylabel(0(0.2)0.4 0.4 "0.4" 0.2 "0.2" 0 "0") ///
		 legend(ring(1) col(3) position(6) size(*0.6) symxsize(small) symysize(small))  /// 
		 yline(0, lpattern(dash) lcolor(black)) /// 
		 coeflabels(lntw = "Ln(1+Weighted Tariff Rate)") ///
		 recast(bar) ciopts(recast(rcap)) citop barwidt(0.1) ///
		 scheme(s1color) name(c, replace) 
graph save c, replace

************************
*2D
use GPS_data.dta, clear
local control "c.age i.gender i.subj_math_skills"

qui reg altruism lntw `control', cluster(region)
qui estimates store tariff_w_gps

qui reg altruism lnts `control', cluster(region)
qui estimates store tariff_s_gps

qui reg altruism import `control', cluster(region)
qui estimates store import_gps

qui reg altruism export `control', cluster(region)
qui estimates store export_gps

coefplot (tariff_w_gps, label("Weighted Tariff Rate (%)")) ///
		 (tariff_s_gps, label("Simple Tariff Rate (%)")) ///
		 (import_gps, label("Imports Rate (%)")) ///
		 (export_gps, label("Exports Rate (%)")) ///
		 , keep(lntw lnts import export) vertical ///
		 title("D") ///
		 ytitle("Effect Size") ///
		 ylabel(-0.8(0.4)0.4 -0.8 "-0.8" -0.4 "-0.4" 0 "0" 0.4 "0.4") ///
		 legend(ring(0) col(1) position(7) size(*0.6) symxsize(small) symysize(small))  /// 
		 yline(0, lpattern(dash) lcolor(black)) /// 
		 coeflabels(lntw = " " ///
					lnts = " " ///
					import = " " ///
					export = " " ) ///
		 recast(bar) ciopts(recast(rcap)) citop barwidt(0.5) ///
		 subtitle("Global Preferences Survey Data (Altruism Index)" " ", size(*0.8) ring(1) position(6)) ///  
		 scheme(s1color) name(d, replace) 
graph save d, replace	

*combine figures
gr combine a.gph b.gph c.gph d.gph , r(2) c(2) scheme(s1mono) saving(figure2, replace)

*************************************************************
**Figure 3
*************************************************************
use wvs_data.dta, clear

global control "female age i.age_g i.education i.marital i.child i.employment i.social_class i.income i.year"
local mech "inequality zero_sum luck_belief"

foreach y in `mech'{
	
	qui reg `y' lntw $control, cluster(country_id)
	qui estimates store `y'
}

************************
*3D
coefplot (inequality, label("Attitudes toward income inequality")) ///
		 (zero_sum, label("Zero-sum thinking")) ///
		 (luck_belief, label("Belief that luck determines income")) ///
		 , keep(lntw) vertical ///
		 title("D") ///
		 ytitle("Effect Size") ///
		 ylabel(-0.12(0.04)0.002 -0.12 "-0.12" -0.08 "-0.08" -0.04 "-0.04") ///
		 legend(ring(0) col(1) position(7) size(*0.4) symxsize(tiny) symysize(tiny))  ///
		 yline(0, lpattern(dash) lcolor(black)) ///
		 coeflabels(lntw = "Ln(1+Weighted Tariff Rate)" ) ///
		 recast(bar) ciopts(recast(rcap)) citop barwidt(0.1) ///
		 scheme(s1color) name(d, replace) 
graph save d, replace

bys year country_id : egen prosocial_m = mean(prosocial)
bys year country_id : egen inequality_m = mean(inequality)
bys year country_id : egen zero_sum_m = mean(zero_sum)
bys year country_id : egen luck_belief_m = mean(luck_belief)
keep year country_id countrycode prosocial_m inequality_m zero_sum_m luck_belief_m
duplicates drop year country_id, force

cap drop tariff_w
merge m:1 countrycode year using tariff.dta, keepusing(tariff_w) nogenerate
drop if tariff_w > 100
drop if prosocial_m == .

************************
*3A-3C  
twoway (scatter inequality_m tariff_w, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci inequality_m tariff_w, level(95) lcolor(gray)), ///
	   legend(off) title("A") ///
	   ylabel(0.6(0.1)1 0.6 "0.6" 0.7 "0.7" 0.8 "0.8" 0.9 "0.9") ///
	   xlabel(0(5)15) ///
	   xscale(range(0 15)) ///
	   ytitle("Attitudes toward income inequality") ///
	   xtitle("Weighted Tariff Rate (%)") ///
	   scheme(s1color) name(a, replace) 
graph save a, replace	

twoway (scatter zero_sum_m tariff_w, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci zero_sum_m tariff_w, level(95) lcolor(gray)), ///
	   legend(off) title("B") ///
	   ylabel(0.4(0.2)1 0.4 "0.4" 0.6 "0.6" 0.8 "0.8") ///
	   xlabel(0(5)15) ///
	   ytitle("Zero-sum thinking") ///
	   xtitle("Weighted Tariff Rate (%)") ///
	   scheme(s1color) name(b, replace) 
graph save b, replace	

twoway (scatter luck_belief_m tariff_w, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci luck_belief_m tariff_w, level(95) lcolor(gray)), ///
	   legend(off) title("C") ///
	   xlabel(0(5)15) ///
	   ylabel(0.2(0.2)1 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8") ///
	   ytitle("Belief that luck determines income") ///
	   xtitle("Weighted Tariff Rate (%)") ///
	   scheme(s1color) name(c, replace) 
graph save c, replace	

*combine figures
gr combine a.gph b.gph c.gph d.gph , r(2) c(2) scheme(s1mono) saving(figure3, replace)

*************************************************************
**Figure 4
*************************************************************
use wvs_data.dta, clear

bys year country_id : egen prosocial_m = mean(prosocial)
bys year country_id : egen inequality_m = mean(inequality)
bys year country_id : egen zero_sum_m = mean(zero_sum)
bys year country_id : egen luck_belief_m = mean(luck_belief)
keep year country_id countrycode prosocial_m inequality_m zero_sum_m luck_belief_m
duplicates drop year country_id, force

cap drop tariff_w
merge m:1 countrycode year using tariff.dta, keepusing(tariff_w) nogenerate
drop if tariff_w > 100
drop if prosocial_m == .

************************
*4A-4C
twoway (scatter prosocial_m inequality_m, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci prosocial_m inequality_m, level(95) lcolor(gray)), ///
	   legend(off) title("A") ///
	   ylabel(3.5(1)5.5) ///
	   xlabel(0.6(0.1)1 0.6 "0.6" 0.7 "0.7" 0.8 "0.8" 0.9 "0.9") ///
	   xtitle("Attitudes toward income inequality") ///
	   ytitle("Average Level of Prosociality") ///
	   scheme(s1color) name(a, replace) 
graph save a, replace

twoway (scatter prosocial_m zero_sum_m, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci prosocial_m zero_sum_m, level(95) lcolor(gray)), ///
	   legend(off) title("B") ///
	   ylabel(3.5(1)5.5) ///
	   xlabel(0.5(0.1)0.9 0.5 "0.5" 0.6 "0.6" 0.7 "0.7" 0.8 "0.8" 0.9 "0.9") ///
	   xtitle("Zero-sum thinking") ///
	   ytitle("Average Level of Prosociality") ///
	   scheme(s1color) name(b, replace) 
graph save b, replace

twoway (scatter prosocial_m luck_belief_m, mlabel() /// 
		mcolor(black) msymbol(circle) msize(small)) ///
       (lfitci prosocial_m luck_belief_m, level(95) lcolor(gray)), ///
	   legend(off) title("C") ///
	   ylabel(3.5(1)5.5)  ///
	   xlabel(0.2(0.2)0.9 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8") ///
	   xtitle("Belief that luck determines income") ///
	   ytitle("Average Level of Prosociality") ///
	   scheme(s1color) name(c, replace) 
graph save c, replace

gr combine a.gph b.gph c.gph, r(1) c(3) scheme(s1mono) saving(abc, replace)

************************
*4D
use wvs_data.dta, clear

local var "prosocial inequality zero_sum luck_belief"
foreach i in `var'{
	bys year country_id : egen `i'_m = mean(`i')
}
duplicates drop year country_id, force
keep year countrycode country_id *_m lntw

qui reg prosocial_m lntw i.year, cluster(country_id)
qui estimates store No_mech

qui reg prosocial_m lntw inequality_m i.year, cluster(country_id)
qui estimates store Mech_inequality

qui reg prosocial_m lntw zero_sum_m i.year, cluster(country_id)
qui estimates store Mech_zero_sum

qui reg prosocial_m lntw luck_belief_m i.year, cluster(country_id)
qui estimates store Mech_luck_belief

coefplot (No_mech, label("Control No Mechanism Var.")) ///
		 (Mech_inequality, label("Control Inequality Attitudes")) ///
		 (Mech_zero_sum, label("Control Zero-sum Thinking")) ///
		 (Mech_luck_belief, label("Control Luck Belief")) ///
		 , keep(lntw) vertical ///
		 title("D") ///
		 ytitle("Effect Size") ///
		 ylabel(0(0.1)0.4 0.4 "0.4" 0.3 "0.3" 0.2 "0.2" 0.1 "0.1") ///
		 legend(ring(1) col(2) position(6) size(*0.8) symxsize(small) symysize(small))  ///
		 yline(0, lpattern(dash) lcolor(black)) /// 
		 coeflabels(lntw = "Ln(1+Weighted Tariff Rate)" ) ///
		 recast(bar) ciopts(recast(rcap)) citop barwidt(0.1) ///
		 scheme(s1color) name(d, replace) 
graph save d, replace

************************
*4E
use mechnism_data.dta,clear

global graph_opts1 ///
  bgcolor(white) graphregion(color(white)) ///
  legend(region(lcolor(none) fcolor(none))) ///
  ylabel(,angle(0) nogrid) ///
  subtitle(, justification(left) color(black) span position(11)) ///
  title(, color(black) span)

global pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'
graph bar inequality zero_sum luck_belief Tariff , ///
          stack nofill ///
          ylabel(${pct}) ///
          legend(order(1 "Attitudes toward income inequality" ///
					   2 "Zero-sum thinking" ///
					   3 "Belief that luck determines income" ///
                       4 "Unexplained") ///
                cols(2) position(6) ///
                symxsize(small) symysize(small) size(small)) ///
          ${graph_opts1} ///
		  title("E") ///
		  bar(4, color(white) lcolor(black) lpattern(solid) lwidth(thin)) ///
          bar(1,lwidth(thin) lcolor(black)) ///
		  bar(2,lwidth(thin) lcolor(black)) ///        
          subtitle("", color(black) size(*0.8) justification(center) position(12)) /// 
		  hori scheme(s1color) saving(e, replace)
graph save e, replace

gr combine d.gph e.gph , r(1) c(2) scheme(s1mono) saving(de, replace)
gr combine abc.gph de.gph , r(2) c(1) scheme(s1mono) saving(figure4, replace)

erase a.gph
erase b.gph
erase c.gph
erase d.gph
erase e.gph
erase ab.gph
erase abc.gph
erase de.gph
